/**
 */
package com.dingpeikeji.quickdeer.core.modules.buse.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSON;
import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.mapper.JsonMapper;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.buse.entity.TCompBuse;
import com.dingpeikeji.quickdeer.core.modules.buse.service.TCompBuseService;

/**
 * 巴士管理Controller
 * @author Zhaoy
 * @version 2016-05-26
 */
@Controller
@RequestMapping(value = "${adminPath}/buse/tCompBuse")
public class TCompBuseController extends BaseController {

	@Autowired
	private TCompBuseService tCompBuseService;
	
	@ModelAttribute
	public TCompBuse get(@RequestParam(required=false) String id) {
		TCompBuse entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tCompBuseService.get(id);
		}
		if (entity == null){
			entity = new TCompBuse();
		}
		return entity;
	}
	
	@RequiresPermissions("buse:tCompBuse:view")
	@RequestMapping(value = {"list", ""})
	public String list(TCompBuse tCompBuse, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TCompBuse> page = tCompBuseService.findPage(new Page<TCompBuse>(request, response), tCompBuse); 
		model.addAttribute("page", page);
		return "modules/buse/tCompBuseList";
	}

	@RequiresPermissions("buse:tCompBuse:view")
	@RequestMapping(value = "form")
	public String form(TCompBuse tCompBuse, Model model) {
		model.addAttribute("tCompBuse", tCompBuse);
		/*JsonMapper mapper = new JsonMapper();
		String tBuseExchgeList = mapper.toJson(tCompBuse.getTBuseExchgeList());*/
		model.addAttribute("tBuseExchgeList", JSON.toJSONString(tCompBuse.getTBuseExchgeList()));
		return "modules/buse/tCompBuseForm";
	}

	@RequiresPermissions("buse:tCompBuse:edit")
	@RequestMapping(value = "save")
	public String save(TCompBuse tCompBuse, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tCompBuse)){
			return form(tCompBuse, model);
		}
		tCompBuseService.save(tCompBuse);
		addMessage(redirectAttributes, "保存巴士管理成功");
		return "redirect:"+Global.getAdminPath()+"/buse/tCompBuse/?repage";
	}
	
	@RequiresPermissions("buse:tCompBuse:edit")
	@RequestMapping(value = "delete")
	public String delete(TCompBuse tCompBuse, RedirectAttributes redirectAttributes) {
		tCompBuseService.delete(tCompBuse);
		addMessage(redirectAttributes, "删除巴士管理成功");
		return "redirect:"+Global.getAdminPath()+"/buse/tCompBuse/?repage";
	}

}
